package com.xdl.moudle.utils.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	private static final Logger logger = LoggerFactory.getLogger(DateUtil.class);

	private static final SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	private static final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
	private static final SimpleDateFormat shortDateFormat = new SimpleDateFormat("yyyyMMdd");
	private static final SimpleDateFormat dateMicrosecondFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
	private static final SimpleDateFormat shortDateMicrosecondFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
	
    /**
     * 格式化yyyy-MM-dd HH:mm:ss.SSS
     * @param date
     * @return
     */
	public static String formatDateMicrosecond(Date date) {
		return dateMicrosecondFormat.format(date);
	}
	/**
	 * 格式化yyyyMMddHHmmssSSS
	 * @param date
	 * @return
	 */
	public static String formatShortDateMicrosecond(Date date) {
		return shortDateMicrosecondFormat.format(date);
	}


	/**
	 * 格式化yyyy-MM-dd HH:mm:ss
	 */
	public static String formatDatetime(Date datetime) {
		return datetimeFormat.format(datetime);
	}

	/**
	 * 格式化yyyy-MM-dd
	 */
	public static String formatDate(Date date) {
		return dateFormat.format(date);
	}

	/**
	 * 格式化成短日期 yyyyMMdd
	 */
	public static String formatShortDate(Date date) {
		return shortDateFormat.format(date);
	}

	/**
	 * 格式化HH:mm:ss
	 */
	public static String formatTime(Date time) {
		return timeFormat.format(time);
	}

	/**
	 * 获取当前时间
	 */
	public static String getCurrentDatetime() {
		return datetimeFormat.format(new Date());
	}

	/**
	 * 获取当前日期
	 */
	public static String getCurrentDate() {
		return dateFormat.format(new Date());
	}

	/**
	 * 获取当前时间
	 */
	public static String getCurrentTime() {
		return timeFormat.format(new Date());
	}
	
	/**
	 * 获取系统当前时间戳
	 * @return
	 */
	public static Long getCurrentTimeMillis() {
		return System.currentTimeMillis();
	}

	/**
	 * 获取系统当前时间
	 * @return
	 * @throws ParseException
	 */
	public static Date getCurrentDateTime() {
		Date date = null;
		try {
			String dateTime = getCurrentDatetime();
			date = datetimeFormat.parse(dateTime);
		} catch (Exception e) {
			logger.error("获取当前日期出错", e);
		}
		return date;
	}

	/**
	 * 解析日期与时间
	 */
	public static Date parseDatetime(String str) {
		Date date = null;
		try {
			date = datetimeFormat.parse(str);
		} catch (ParseException e) {
			logger.error("解析日期字符串出错！格式：yyyy-MM-dd HH:mm:ss", e);
		}
		return date;
	}

	/**
	 * 解析日期
	 */
	public static Date parseDate(String str) {
		Date date = null;
		try {
			date = dateFormat.parse(str);
		} catch (ParseException e) {
			logger.error("解析日期字符串出错！格式：yyyy-MM-dd", e);
		}
		return date;
	}

	/**
	 * 解析时间
	 */
	public static Date parseTime(String str) {
		Date date = null;
		try {
			date = timeFormat.parse(str);
		} catch (ParseException e) {
			logger.error("解析日期字符串出错！格式：HH:mm:ss", e);
		}
		return date;
	}

	/**
	 * 解析短时间
	 */
	public static Date parseShortDate(String str) {
		Date date = null;
		try {
			date = shortDateFormat.parse(str);
		} catch (ParseException e) {
			logger.error("解析日期字符串出错！格式：yyyyMMdd", e);
		}
		return date;
	}

	/**
	 * 获取N天后的当前时间
	 */
	public static Date getNextDay(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, n);
		return cal.getTime();
	}

	/**
	 * 获取 N天后的最小时间
	 */
	public static Date getNextMinDay(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, n);
		cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), 0, 0, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 获取 N天后的最大时间
	 */
	public static Date getNextMaxDay(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, n);
		cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), 23, 59, 59);
		cal.set(Calendar.MILLISECOND, 999);
		return cal.getTime();
	}

	/**
	 * -1：startTime小于endTime,0:startTime=endTime,1:startTime>endTime
	 * 
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static int compareDate(Date startTime, Date endTime) {
		int iSize = 0;
		try {
			if (startTime.getTime() > endTime.getTime()) {
				iSize = 1;
			} else if (startTime.getTime() < endTime.getTime()) {
				iSize = -1;
			} else {
				iSize = 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return iSize;
	}
	
	// 是否同一天
	public static boolean isSomeDay(Date startTime, Date endTime) {
		boolean isFL = false;
		try {
			String startDate = DateUtil.formatDate(startTime);
			String endDate  = DateUtil.formatDate(endTime);
			
			if (endDate.equals(startDate)) {
				isFL = true;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return isFL;
	}
	
	public static Long hourToSec(double hour) {
		return  (long) (hour * 3600);
	}
	
	public static Long MinuteToSec(Integer  minute) {
		return  (long) (minute * 60);
	}
	
	public static String secToTime(Long sec) {  
        String timeStr = null;  
        int hour = 0;  
        int minute = 0;  
        int second = 0;  
        if (sec <= 0)  
            return "00:00";  
        else {  
            minute = (int) (sec / 60);  
            if (minute < 60) {  
                second = (int) (sec % 60);  
                timeStr = unitFormat(minute) + ":" + unitFormat(second);  
            } else {  
                hour = minute / 60;  
                minute = minute % 60;  
                second = (int) (sec - hour * 3600 - minute * 60);  
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);  
            }  
        }  
        return timeStr;  
    }  
	
	public static String unitFormat(int i) {  
        String retStr = null;  
        if (i >= 0 && i < 10)  
            retStr = "0" + Integer.toString(i);  
        else  
            retStr = "" + i;  
        return retStr;  
    }  
	






	

}
